from db import db_handler
from interface import bank_interface
from lib import common

shop_logger = common.get_logger('shop')

# 商城结账接口
def buy_shop_interface(user,cost):

    flag = bank_interface.pay_interface(user,cost)

    if flag:
        shop_logger.info(f'{user}购物成功！,花费{cost}元')

        return True, f'{user}购物成功！,花费{cost}元'

    else:
        shop_logger.info(f'{user}余额不足，支付失败！')
        return False, f'{user}余额不足，支付失败！'





# 添加购物车接口
def add_shop_cart_interface(user,shop_cart):

    user_dic = db_handler.select(user)

    old_cart = user_dic['shop_cart']

    # 循环当前购物车接口
    for old_good in shop_cart:
        if old_good in old_cart:
            # 获取当前购物车中的商品数量
            number = shop_cart[old_good]

            # 给用户信息中的商品数量 做 增量运算
            old_cart[old_good] += number

        else:
            #获取当前购物车中的商品数量
            number = shop_cart[old_good]

            old_cart[old_good] = number

    user_dic['shop_cart'].update(old_cart)
    db_handler.save(user_dic)
    shop_logger.info(f'{user}添加购物车成功')
    return True, f'{user}添加购物车成功'
add_shop_cart_interfaceshopping_logger = common.get_logger('add_shop_cart_interface')


def check_shop_interface(user):
    user_dic = db_handler.select(user)
    old_shop_cart = user_dic['shop_cart']
    return old_shop_cart









